package logic

import (
	"context"
	"errors"
	"video-joy/models/mysql"

	"video-joy/userrpc/internal/svc"
	"video-joy/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectionAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectionAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectionAddLogic {
	return &CollectionAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 收藏添加
func (l *CollectionAddLogic) CollectionAdd(in *userrpc.CollectionAddRequest) (*userrpc.CollectionAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.UserInfo

	if err := user.UserFindById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("请先登录")
	}

	var collections mysql.CollectionInfo

	if err := collections.CollectionFindById(mysql.DB, in.UserId, in.VideoId); err != nil {
		return nil, err
	}
	if collections.ID != 0 {
		return nil, errors.New("该视频已收藏")
	}

	collection := &mysql.CollectionInfo{
		UserId:        int(in.UserId),
		VideoId:       int(in.VideoId),
		CollectStatus: int(in.CollectStatus),
	}

	if err := collection.CollectionAdd(mysql.DB); err != nil {
		return nil, errors.New("收藏失败")
	}

	return &userrpc.CollectionAddResponse{
		CollectionId: int64(collection.ID),
	}, nil
}
